<!DOCTYPE html>
<HTML>
<HEAD><meta name="viewport" content="width=device-width, initial-scale=1">
<Title>Simple Zooming and Scrolling</Title>
<link type='text/css' rel='Stylesheet' href="maxchartapi.css" />
</HEAD>
<body bgcolor="#FFFFFF" text="#000000" topmargin="0" leftmargin="0" rightmargin="0" marginwidth="0" marginheight="0">
<p class="heading0">ChartDirector 7.0 (Java Edition)</p>
<p class="heading1">Simple Zooming and Scrolling</p>
<hr class="separator">
<div class="content"><img src="images/simplezoomscroll_web.png" width='762' height='447'><br><br>
This sample program demonstrates simple zoomable and scrollable chart with tooltips, using mouse click and drag to control zooming and scrolling.<br><br>
The main source code listing of this sample program is included at the end of this section. The code consists of the following main parts:<br><br>
<ul>
<li><b>initViewer</b>: This function is for initializing the <a href="WebChartViewer.htm">WebChartViewer</a> during initial page request. It sets up the full data range of the view port, the initial visible range, as well as the maximum zoom in ratio allowed.<br><br>
<li><b>drawChart</b>: This is the main charting code. It first gets the visible data range from the view port using <a href="WebChartViewer.getValueAtViewPort.htm">WebChartViewer.getValueAtViewPort</a>. Then it gets the data in that data range. After that it plots the chart as usual, with the x-axis configured to synchronize with the view port using <a href="WebChartViewer.syncDateAxisWithViewPort.htm">WebChartViewer.syncDateAxisWithViewPort</a>.<br><br>
<li><b>HTML</b>: The HTML part of the code implements the web page. <a href="WebChartViewer.renderHTML.htm">WebChartViewer.renderHTML</a> is used to generate the tags for the chart image. Javascript code is used in the onclick handler of the radio buttons to allow them to control the mouse usage.<br><br>
<li><b>window onload event handler</b>: The event handler, set up using <a href="JsChartViewer.addEventListener.htm">JsChartViewer.addEventListener</a>, is used to initialize the browser side user interface. It configures the <a href="JsChartViewer.ViewPortChanged.htm">JsChartViewer.ViewPortChanged</a> event to trigger an AJAX chart update. The  the mouse usage is initialized to <a href="JsChartViewer.Scroll.htm">JsChartViewer.Scroll</a> for drag to scroll.<br></ul>
When the script starts to run , the first thing it does is to use <a href="WebChartViewer.isPartialUpdateRequest.htm">WebChartViewer.isPartialUpdateRequest</a> to detect if it is handling an AJAX chart update or an initial page request. If it is an AJAX chart update, it just draws the chart and sends back an AJAX response using <a href="WebChartViewer.partialUpdateChart.htm">WebChartViewer.partialUpdateChart</a>, then terminates the request. If it is an initial page request, it initializes the <a href="WebChartViewer.htm">WebChartViewer</a>, draws the chart, and sends the HTML contents to the browser as usual.
</div><p class="heading1a">Files in this Demonstration</p><div class="content">
This sample script requires the following files. When developing your own script using this sample script as a template, please ensure you copy the necessary files to the proper directories (typically the same directory as the script) and reference them using proper paths.<br><br>
<div style="width:100%;box-sizing:border-box;"><table width="100%" border="1" cellpadding="5" cellspacing="0"> <tr><th width="24%">File<th>Description
<tr><td>simplezoomscroll.jsp<td>Main source code for this demo.
<tr><td>cdjcv.js<br>zoomin.cur<br>zoomout.cur<br>wait.gif<td>The script file for <a href="JsChartViewer.htm">JsChartViewer</a>, and the supporting cursor and image files. They must be in the same directory.
</table></div></div><p class="heading1a">Source Code Listing</p><div class="content">
<b>[JSP Version]</b> jspdemo/simplezoomscroll.jsp
<div class='codeblock'><code class='pre'>&lt;%@page import="ChartDirector.*, java.util.*" %&gt;
&lt;%!
//
// Initialize the WebChartViewer when the page is first loaded
//
private void initViewer(WebChartViewer viewer)
{
    // The full x-axis range is from Jan 1, 2007 to Jan 1, 2012
    Date startDate = new GregorianCalendar(2010, 0, 1).getTime();
    Date endDate = new GregorianCalendar(2015, 0, 1).getTime();
    viewer.setFullRange("x", startDate, endDate);

    // Initialize the view port to show the last 366 days (out of 1826 days)
    viewer.setViewPortWidth(366.0 / 1826);
    viewer.setViewPortLeft(1 - viewer.getViewPortWidth());

    // Set the maximum zoom to 10 days (out of 1826 days)
    viewer.setZoomInWidthLimit(10.0 / 1826);
}

//
// Create a random table for demo purpose.
//
private RanTable getRandomTable()
{
    RanTable r = new RanTable(127, 4, 1828);
    r.setDateCol(0, new GregorianCalendar(2010, 0, 1).getTime(), 86400);
    r.setCol(1, 150, -10, 10);
    r.setCol(2, 200, -10, 10);
    r.setCol(3, 250, -8, 8);
    return r;
}

//
// Draw the chart
//
private void drawChart(WebChartViewer viewer)
{
    // Determine the visible x-axis range
    Date viewPortStartDate = Chart.NTime(viewer.getValueAtViewPort("x", viewer.getViewPortLeft()));
    Date viewPortEndDate = Chart.NTime(viewer.getValueAtViewPort("x", viewer.getViewPortLeft() +
        viewer.getViewPortWidth()));

    // We need to get the data within the visible x-axis range. In real code, this can be by using a
    // database query or some other means as specific to the application. In this demo, we just
    // generate a random data table, and then select the data within the table.
    RanTable r = getRandomTable();

    // Select the data for the visible date range viewPortStartDate to viewPortEndDate. It is
    // possible there is no data point at exactly viewPortStartDate or viewPortEndDate. In this
    // case, we also need the data points that are just outside the visible date range to "overdraw"
    // the line a little bit (the "overdrawn" part will be clipped to the plot area) In this demo,
    // we do this by adding a one day margin to the date range when selecting the data.
    r.selectDate(0, new Date(viewPortStartDate.getTime() - 86400000), new Date(
        viewPortEndDate.getTime() + 86400000));

    // The selected data from the random data table
    Date[] timeStamps = Chart.NTime(r.getCol(0));
    double[] dataSeriesA = r.getCol(1);
    double[] dataSeriesB = r.getCol(2);
    double[] dataSeriesC = r.getCol(3);

    //
    // Now we have obtained the data, we can plot the chart.
    //

    //================================================================================
    // Configure overall chart appearance.
    //================================================================================

    // Create an XYChart object 600 x 300 pixels in size, with pale blue (f0f0ff) background, black
    // (000000) rounded border, 1 pixel raised effect.
    XYChart c = new XYChart(600, 300, 0xf0f0ff, 0x000000);
    c.setRoundedFrame();

    // Set the plotarea at (52, 60) and of size 520 x 205 pixels. Use white (ffffff) background.
    // Enable both horizontal and vertical grids by setting their colors to grey (cccccc). Set
    // clipping mode to clip the data lines to the plot area.
    c.setPlotArea(55, 60, 520, 205, 0xffffff, -1, -1, 0xcccccc, 0xcccccc);

    // As the data can lie outside the plotarea in a zoomed chart, we need to enable clipping.
    c.setClipping();

    // Add a top title to the chart using 15pt Times New Roman Bold Italic font, with a light blue
    // (ccccff) background, black (000000) border, and a glass like raised effect.
    c.addTitle("Product Line International Market Price", "Times New Roman Bold Italic", 15
        ).setBackground(0xccccff, 0x000000, Chart.glassEffect());

    // Add a legend box at the top of the plot area with 9pt Arial Bold font with flow layout.
    c.addLegend(50, 33, false, "Arial Bold", 9).setBackground(Chart.Transparent, Chart.Transparent);

    // Set axes width to 2 pixels
    c.xAxis().setWidth(2);
    c.yAxis().setWidth(2);

    // Add a title to the y-axis
    c.yAxis().setTitle("Price (USD)", "Arial Bold", 10);

    //================================================================================
    // Add data to chart
    //================================================================================

    //
    // In this example, we represent the data by lines. You may modify the code below to use other
    // representations (areas, scatter plot, etc).
    //

    // Add a line layer for the lines, using a line width of 2 pixels
    LineLayer layer = c.addLineLayer2();
    layer.setLineWidth(2);

    // In this demo, we do not have too many data points. In real code, the chart may contain a lot
    // of data points when fully zoomed out - much more than the number of horizontal pixels in this
    // plot area. So it is a good idea to use fast line mode.
    layer.setFastLineMode();

    // Now we add the 3 data series to a line layer, using the color red (ff0000), green (00cc00)
    // and blue (0000ff)
    layer.setXData(timeStamps);
    layer.addDataSet(dataSeriesA, 0xff0000, "Product Alpha");
    layer.addDataSet(dataSeriesB, 0x00cc00, "Product Beta");
    layer.addDataSet(dataSeriesC, 0x0000ff, "Product Gamma");

    //================================================================================
    // Configure axis scale and labelling
    //================================================================================

    // Set the x-axis as a date/time axis with the scale according to the view port x range.
    viewer.syncDateAxisWithViewPort("x", c.xAxis());

    // In this demo, we rely on ChartDirector to auto-label the axis. We ask ChartDirector to ensure
    // the x-axis labels are at least 75 pixels apart to avoid too many labels.
    c.xAxis().setTickDensity(75);

    //================================================================================
    // Output the chart
    //================================================================================

    // Output the chart
    viewer.setChart(c, Chart.SVG);

    // Include tool tip for the chart
    viewer.setImageMap(c.getHTMLImageMap("", "",
        "title='[{dataSetName}] {x|mmm dd, yyyy}: USD {value|2}'"));
}
%&gt;
&lt;%

//
// This script handles both the full page request, as well as the subsequent partial updates (AJAX
// chart updates). We need to determine the type of request first before we processing it.
//

// Create the WebChartViewer object
WebChartViewer viewer = new WebChartViewer(request, "chart1");

if (viewer.isPartialUpdateRequest()) {
    // Is a partial update request. Draw the chart and perform a partial response.
    drawChart(viewer);
    out.clear();
    viewer.partialUpdateChart(response);
    return;
}

//
// If the code reaches here, it is a full page request.
//

// In this exapmle, we just need to initialize the WebChartViewer and draw the chart.
initViewer(viewer);
drawChart(viewer);
%&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Simple Zooming and Scrolling&lt;/title&gt;
    &lt;script type="text/javascript" src="cdjcv.js"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body style="margin:0px;"&gt;
&lt;script type="text/javascript"&gt;

//
// Execute the following initialization code after the web page is loaded
//
JsChartViewer.addEventListener(window, 'load', function() {
    // Update the chart when the view port has changed (eg. when the user zooms in using the mouse)
    var viewer = JsChartViewer.get('&lt;%=viewer.getId()%&gt;');
    viewer.attachHandler("ViewPortChanged", viewer.partialUpdate);

    // Set the initial mouse usage to "scroll"
    viewer.setMouseUsage(JsChartViewer.Scroll);
    document.getElementById("scrollChart").checked = true;
});

&lt;/script&gt;
&lt;form method="post"&gt;
&lt;table cellspacing="0" cellpadding="0" border="0"&gt;
    &lt;tr&gt;
        &lt;td align="right" colspan="2" style="background:#000088"&gt;
            &lt;div style="padding:0px 3px 2px 0px; font:italic bold 10pt Arial;"&gt;
                &lt;a style="color:#FFFF00; text-decoration:none" href="http://www.advsofteng.com/"&gt;Advanced Software Engineering&lt;/a&gt;
            &lt;/div&gt;
        &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr valign="top"&gt;
        &lt;td style="width:127px; background:#c0c0ff; border-right:black 1px solid; border-bottom:black 1px solid;"&gt;
        &lt;div style="font:9pt Verdana; padding:10px 0px 0px 3px; line-height:1.5; width:127px"&gt;
            &lt;!-- The onclick handler of the following radio buttons sets the mouse usage mode. --&gt;
            &lt;input name="mouseUsage" id="scrollChart" type="radio"
                onclick="JsChartViewer.get('&lt;%=viewer.getId()%&gt;').setMouseUsage(JsChartViewer.Scroll)" /&gt;
            Drag To Scroll&lt;br /&gt;
            &lt;input name="mouseUsage" id="zoomInChart" type="radio"
                onclick="JsChartViewer.get('&lt;%=viewer.getId()%&gt;').setMouseUsage(JsChartViewer.ZoomIn)" /&gt;
            Zoom In&lt;br /&gt;
            &lt;input name="mouseUsage" id="zoomOutChart" type="radio"
                onclick="JsChartViewer.get('&lt;%=viewer.getId()%&gt;').setMouseUsage(JsChartViewer.ZoomOut)" /&gt;
            Zoom Out&lt;br /&gt;
        &lt;/div&gt;
        &lt;/td&gt;
        &lt;td&gt;
            &lt;div style="font-weight:bold; font-size:20pt; margin:5px 0px 0px 5px; font-family:Arial"&gt;
                Simple Zooming and Scrolling
            &lt;/div&gt;
            &lt;hr style="border:solid 1px #000080" /&gt;
            &lt;div style="padding:0px 5px 5px 10px"&gt;
                &lt;!-- ****** Here is the chart image ****** --&gt;
                &lt;%=viewer.renderHTML(response)%&gt;
            &lt;/div&gt;
        &lt;/td&gt;
    &lt;/tr&gt;
&lt;/table&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;</code></div></div><br>
<hr class="separator"><div class="copyright">&copy; 2022 Advanced Software Engineering Limited. All rights reserved.</div></body>
</HTML>
